/*
 * @Author: C.
 * @Date: 2021-06-17 10:09:27
 * @LastEditTime: 2021-06-17 10:16:53
 * @Description: file content
 */
// 日期过滤
const DateFilter = (date, fmt = 'yyyy-MM-dd') => {
  if (!date || date === '') return '--'
  var currentDate = new Date(date)
  var o = {
    'M+': currentDate.getMonth() + 1, // 月份
    'd+': currentDate.getDate(), // 日
    'h+': currentDate.getHours(), // 小时
    'm+': currentDate.getMinutes(), // 分
    's+': currentDate.getSeconds(), // 秒
    'q+': Math.floor((currentDate.getMonth() + 3) / 3), // 季度
    'S': currentDate.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (currentDate.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }

  return fmt
}

const realTimeHandle = (time, fmt = 'yyyy-MM-dd') => {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  // 传入时间
  const d = new Date(time)
  // 当前
  const now = Date.now()

  const diff = (now - d) / 1000
  // 3600 一个小时
  let tips = ''
  if (diff < 3600 * 24) {
    tips = DateFilter(d, 'hh:mm')
  } else if (diff < 3600 * 24 * 7) {
    const week = ['日', '一', '二', '三', '四', '五', '六']
    tips = '周' + week[new Date(d).getDay()]
  } else {
    tips = DateFilter(d, fmt)
  }
  return tips
}
export {
  DateFilter,
  realTimeHandle
}
